<?php

class DetailKipemsController extends \BaseController {

	/**
	 * Display a listing of detailkipems
	 *
	 * @return Response
	 */
	public function index()
	{
		$detailkipems = DB::table('detail_kipems')->leftJoin('provinsis','provinsis.id','=','detail_kipems.provinsi_id')->where('detail_kipems.is_active','=','1')->orderBy('provinsis.nama','ASC')->get(array('detail_kipems.*','provinsis.nama AS nama_prov'));

		return View::make('detail_kipems.index', compact('detailkipems'));
	}

	/**
	 * Show the form for creating a new detailkipem
	 *
	 * @return Response
	 */
	public function create()
	{
		// return View::make('detail_kipems.create');

		// $provinsis = array('' => '- - Pilih Provinsi - -') + Provinsi::where('is_active', '=', '1')->orderBy('nama','ASC')->lists('nama','id');
		return View::make('detail_kipems.create');
	}

	/**
	 * Store a newly created detailkipem in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		$validator = Validator::make($data = Input::all(), Detailkipem::$rules);

		if ($validator->fails())
		{
			return Redirect::back()->withErrors($validator)->withInput();
		}

		Detailkipem::create($data);

		return Redirect::route('detail_kipems.index');
	}

	/**
	 * Display the specified detailkipem.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$detailkipem = Detailkipem::findOrFail($id);

		return View::make('detail_kipems.show', compact('detailkipem'));
	}

	/**
	 * Show the form for editing the specified detailkipem.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		// $provinsis = Provinsi::where('is_active', '=', '1')->orderBy('nama','ASC')->lists('nama','id');
		// return View::make('kabupatens.edit', array('provinsis'=>$provinsis));

		$detailkipem = Detailkipem::find($id);
		// return View::make('kabupatens.edit', compact('kabupaten'));
		return View::make('detail_kipems.edit')->with(compact('detailkipem'));
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		$validator = Validator::make($data = Input::all(), Detailkipem::$rules);

		if ($validator->fails())
		{
			return Redirect::back()->withErrors($validator)->withInput();
		}

		Detailkipem::create($data);

		$detailkipem = Detailkipem::findOrFail($id);

		// $validator = Validator::make($data = Input::all(), Detailkipem::$rules);

		// if ($validator->fails())
		// {
		// 	return Redirect::back()->withErrors($validator)->withInput();
		// }

		// $detailkipem->update($data);

		$detailkipem->is_active = 0;

		$detailkipem->save();

		return Redirect::route('detail_kipems.index');
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		// Detailkipem::destroy($id);

		$detailkipem = Detailkipem::findOrFail($id);
		$detailkipem->is_active = 0;
		$detailkipem->user_id = Auth::user()->id;
		$detailkipem->action = 3;

		$detailkipem->save();

		return Redirect::route('detail_kipems.index');
	}

}